package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        /*
         SELECT t.name,t.salary,t.title,
         FROM class c,teacher t
         WHERE c.teacher_id= t.id
         AND t.name = '王克晶'
          */
        try(Connection connection = DBUtil.getConnection();) {
            Statement statement = connection.createStatement();
            String sql = "SELECT t.name,t.salary,t.title,c.name,c.floor\n" +
                    "         FROM class c,teacher t\n" +
                    "         WHERE c.teacher_id= t.id\n" +
                    "         AND t.name = '王克晶'";
            ResultSet re = statement.executeQuery(sql);
            while(re.next()){
                String t_name = re.getString("t.name");
                int t_salary = re.getInt("t.salary");
                String t_title = re.getString("title");
                String c_name = re.getString("c.name");
                String c_floor = re.getString("c.floor");

                //使用字段索引获取字段值，索引从1开始，不推荐
                String t_name1 = re.getString(1);
                int t_salary1 = re.getInt(2);
                String t_title1 = re.getString(3);
                String c_name1 = re.getString(4);
                String c_floor1 = re.getString(5);

                System.out.println(t_name+"\t"+t_salary+"\t"+t_title+"\t"+c_name+"\t"+c_floor);


            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
